package com.tao.helloword;

import com.rabbitmq.client.*;
import com.utils.RabbitMQUtils;
import org.junit.Test;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @ClassName ConsumerTest
 * @Author HETAO
 * @Date 2020/5/4 18:27
 */
public class ConsumerTest {
    //消费方
    public static void main(String[] args) throws IOException, TimeoutException {
        //工具类获取对象
        Connection connection = RabbitMQUtils.getConnection();
        //创建通道
        Channel channel = connection.createChannel();
        //通道绑定队列
        channel.queueDeclare("hello", true, false, false, null);
        /**
         *通过通道获取消息
         * 参数1:队列名称
         * 参数2:开启消息的自动确认机制
         * 参数3:消费时回调函数
         */
        channel.basicConsume("hello", true, new DefaultConsumer(channel) {
            @Override //最后一个参数:消息队列中取出的消息
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("body = " + new String(body));
            }
        });
    }
}
